/**
 * 发送医嘱
 */
 
//校验材料库存接口  ,有参数控制
function checkCailiaoToreNew(){
	var def = $.Deferred();
	var bby01Arr4 = getBby01Arr4Data();
	bby01Arr_4 = bby01Arr4.bby01Arr_4;
	if (valueArr['p_9999_9999_106']=='1'){
		$.ajax({
			url : rootUrl + "/doctoradviceeditor/checkdrugstoreNew_cailiao",
			data :
			{
			bck01_c : valueArr['p_100_105002_404']=="-1"?bck01:valueArr['p_100_105002_404'],
			lACF01 : 1,
			bby01Arr_4 : JSON.stringify(bby01Arr_4)
			},
			dataType : "json",
			type : "post",
			async : false,
			success : function(data){
				var list = data.list;
				def.resolve(list);
			}
		});
	}
	 return def.promise();
}
//校验药品库存接口  ,没有参数控制
function checkDrugsToreNew(){
	var def = $.Deferred();
	var bby01Arr1 = getBby01Arr1Data();
	var bby01Arr_1 = bby01Arr4.bby01Arr_1;
	var url = rootUrl + "/doctoradviceeditor/checkdrugstoreNew";
	var params = {bck01_x:valueArr['p_100_105002_108'],bck01_z:valueArr['p_100_105002_110']};
	params.lACF01 = 1;
	params.bby01Arr_1 = JSON.stringify(bby01Arr_1);
	$.post(url, params, function(data)
	{
		var list = data.list;
		def.resolve(list);
	});
	return def.promise();
}
//编辑前删除
function deleteBeforeEdit(){
	var def = $.Deferred();
	var vaf01s = "";
	var cbm01 = $("#cbm01").val();
	if (cbm01){
		var editArr = $(window.parent.document).find(".yizhu_details");
		for (var i = 0; i < editArr.length; i++)
		{
			var editStr = $(editArr[i]).attr("data-info");
			if(editStr){
				var editObj = JSON.parse(editStr);
				if (editObj.cbm01 == cbm01)
				{
					vaf01s += editObj.vaf01 + ",";
				}
			}
		}
		var url = rootUrl + "/clideladvice/removeAdvice";
		var data ={
					"vaf01s" : vaf01s,
					"bce03" : bce03
				  };
		$.ajax({
			url : url,
			type : "post",
			data : data,
			dataType : 'json',
			success : function(data)
			{
				def.resolve(data);
			}
		});
	}
	 return def.promise();
}

//保存医嘱接口

//材料校验库存数据组装
function getBby01Arr4Data(){
	//材料
	var bby01Arr_4 = [];
	var bby01Arr_41 = [];// 储存材料需要数量和库存数量的json数组
	var cailiao_tables = $(".left-content").find(".cailiao_table");
	$(cailiao_tables).each(function(x){
		var tableObj = $(this);
		$(tableObj).find("tr").each(function()
		{
			var bby01 = $(this).find(".cailiao_yznr").next().next().val();
			if (bby01 != "" && bby01 != undefined)
			{
				var vaf21 = $(this).find(".cailiao_zl").val(); // 材料数量
				var zxks = $(this).find(".cailiao_zxks").val(); // 材料执行科室
				var ypmc = $(this).find(".cailiao_yznr").val().replace(/[\r\n]/g,"");
				var temp = {};
				temp.bby01 = bby01;
				temp.vaf21 = vaf21;
				temp.ypmc = ypmc;
				temp.zxks = zxks;
				bby01Arr_41.push(temp);
				bby01Arr_4.push(temp);
			}
		});
	});
	var obj = {};
	obj.bby01Arr_4 = bby01Arr_4;
	obj.bby01Arr_41 = bby01Arr_41;
	return obj;
}
//西药中药校验库存数据组装
function getBby01Arr1Data(){
	var bby01Arr_1 = [];
	var bby01Arr_11 = [];// 储存西、成药品需要数量和库存数量的json数组
    // 西药
    var xiyao_tables = $(".left-content").find(".xiyao_table");
	$(xiyao_tables).each(function(x){
		var tableObj = $(this);
		$(tableObj).find("tbody tr").each(function()
		{
			var bby01 = $(this).find(".xiyao_yznr").next().next().val();
			if (bby01 != "" && bby01 != undefined)
			{
				var ypmc = $(this).find(".xiyao_yznr").val().replace(/[\r\n]/g,"");
				var vaf21 = $(this).find(".xiyao_zl").val(); // 数量
				var temp = {};
				temp.bby01 = bby01;
				temp.vaf21 = vaf21;
				temp.ypmc = ypmc;
				temp.zxks = $(this).find(".xiyao_zxks").attr("id");
				bby01Arr_11.push(temp);
				bby01Arr_1.push(temp);
			}
		});
	});
	// 中药
	var zhongyao_tables = $(".left-content").find(".zhongyao_table");
	$(zhongyao_tables).each(function(x){
		var tableObj = $(this);
		$(tableObj).find("tr").each(function()
		{
			var bby01 = $(this).find(".zhongyao_yznr").next().next().val();
			if (bby01 != "" && bby01 != undefined)
			{
				var vaf21 = $(this).find(".zhongyao_zl").val(); // 中药数量
				var ypmc = $(this).find(".zhongyao_yznr").val().replace(/[\r\n]/g,"");
				var temp = {};
				temp.bby01 = bby01;
				temp.vaf21 = vaf21;
				temp.ypmc = ypmc;
				temp.zxks = $(this).parents(".china_medicine").find(".china_medicine_com").find(".zhongyao_zxks select").val();
				bby01Arr_11.push(temp);
				bby01Arr_1.push(temp);
			}
		});
	});
	var obj = {};
	obj.bby01Arr_1 = bby01Arr_1
	obj.bby01Arr_11 = bby01Arr_11
	return obj;  //返回数据
}
//页面数据校验及病历跨域保存
function islegalData(){
	execIframe(); //病历跨域保存
	//成都中药数据校验
	var illageflag = false;
	$("#new_order_list").find("li").find(".messageSign").each(function(ind,ele){
		if($(ele).is(":visible")&&$(ele)[0].style.backgroundColor == "red"){
			obj.illageflag = true;
			obj.msg = "请检查手工输入的医嘱!";
		}
	})
	return illageflag;
}



//直接发送，保存和发送一起执行
function sendAdviceImmediately(obj){
	debugger;
	totalVbi = 0.0;
	var list = obj.list,zjAddList = obj.zjAddList;
	var vaa01 = $("#vaa01").val();
	var vac01 = $("#vac01").val();
	var sendD = obj.data1;  //保存的数据
	sendD.zjAddList = zjAddList;
	sendD.savePrintList = savePrintList;
	sendD.lSign = 2;
	sendD.lBCE01 = bce01;
	sendD.lBCE02 = "";
	sendD.lBCE03 = bce03;
	sendD.lBCK01 = bck01;
	sendD.lACF01 = 1;
	sendD.lVAA07 = vac01;
	sendD.lVAA01 = vaa01;
	var params = {
			params:JSON.stringify(sendD)
	}
	$.when(
			sendAdvicePoint(params) 
     ).done(function (data) {
    	 debugger;
				if (data.result == "Success")
				{
					savePrintList = data.list;
					var cbm01Arr = data.data;
					// 开始更新医嘱抄送记录和生成诊疗申请单
					var temp_vbq = {};
					temp_vbq.vbq27 = $(".e_jianchamudi").val();
					temp_vbq.vbq28 = $(".e_linchuangzhenduan").val();
					temp_vbq.vbq29 = $(".e_binglizhaiyao").val();
					temp_vbq.vbq30 = $(".e_fuzhujiancha").val();
					var url9 = rootUrl + "/clidoctor/nurse_Lis_Bills_Print_batch";
					var params9 = {};
					params9.bce03 = bce03;
					params9.cbm01Arr = JSON.stringify(cbm01Arr);
					params9.vbq1 = JSON.stringify(temp_vbq);
					$.ajax(
					{
					url : url9,
					type : 'post',
					dataType : 'json',
					data : params9,
					success : function(data9)
					{
						$(thisbutton).removeAttr("disabled");
						layer.msg('发送成功',
								{
								time : 3000,
								icon : 1,
								skin : 'layui-layer-msg1'
								});
						// 如果是一卡通扣费用户，执行扣费操作
						var vipId = $("#vipId").text();
						if(false)
//						if (valueArr['p_9999_9999_112']=='1' && vipId != "--")
						{
							var msg = "";
							if ($("#vbu14").text() * 1 < totalVbi)
							{
								msg = "卡内基本余额：" + $("#vbu14").text() + "；本次消费总额：" + totalVbi + "。请先充值。。。";
								layer.alert(msg,
								{
								icon : 1,
								title : "温馨提示："
								});
								return true;
							}
							else
							{
								vbu09 = newCardOut(2);// 弹出消费窗口
								vajList = data.rows;
							}
							
						}
						else
						{
							// 勾选直接打印调用
							var flag = $("#send_is_print").parent().attr("class").indexOf("checked")>=0;
							//判断是否关闭新开界面
							debugger;
							var flag_url = getUrlParam("yiyuanId");
							var lvac01 = $("#vac01").val();
							if(valueArr['p_100_105002_402']=="0"||flag_url)
							{
								if(data.commList&&data.commList.length>0){
									tipCardMsg(data.commList);
								}
								printIm(flag,savePrintList);
								layer.close(zhenjin_index);
								parent.reloadPatientInfo(lvac01);
								$(thisbutton).removeAttr("disabled");
								loadEmrInit();
								isInfectionsDisease(getDiagnoseSql()); //传染病报告卡
							}
							else
							{
								debugger
								var sobj = {};
								sobj.vac01 = lvac01;
								sobj.flag = flag;
								sobj.list = savePrintList;
								sobj.sql = getDiagnoseSql();
								if(data.commList&&data.commList.length>0){
									sobj.msg = feeTipMsg(data.commList);
									parent.closeAndPrint(sobj);
									
								}else{
									sobj.msg = false; 
									parent.closeAndPrint(sobj);
								}
							}
//							$("#sendAdvice_btn").trigger("click");
						}
//						var selectNameMz = getUrlParam("selectNameMz");
//						if(selectNameMz){
//							sendMsgToShoushu(selectNameMz);
//						}
					}
					});
				}
				else
				{
					layer.alert(data.resultMsg,
					{
					icon : 2,
					});
					$(thisbutton).removeAttr("disabled");
				}
			
        	
     });
	
	
}

//直接发送接口
function sendAdvicePoint(params){
	var def = $.Deferred();
	$.ajax({
		url : rootUrl + "/clisendadvice/clinicadvicesendpretreat",
		data :params,
		dataType : "json",
		type : "post",
		success : function(data){
			def.resolve(data);
		}
	});
	 return def.promise();

}























